<?php
/**
 * 素材控制器
 */
namespace Admin\Controller;
use Think\Controller;
class MaterialController extends InitController {
	
	
	//素材列表
	public function upload_material_list(){
		
		$status = I('status','all');
		
		//状态
		if($status == 'all'){
			$where = '';
		}else if(!empty($status) || $status == '0'){
			$where = ' and uum.is_pass = '.$status.'';
		}
		
		
		$topmenu = $this->topmenu;
		$sidebarmenu = $this->sidebarmenu[I('get.k')];
		
		//总数
		$total = D('UploadMaterial')->upload_material_total();
			
		$Page = $this->setPageHander($total);
		$show = $Page?$Page->show():'';	
		$list = D('UploadMaterial')->upload_material_list($where,$Page->firstRow.','.$Page->listRows);
		
		
		$this->assign('list',$list);
		$this->assign('pager',$show);
		$this->assign('topmenu',$topmenu);
		$this->assign('sidebarmenu',$sidebarmenu);
		$this->assign('status',$status);
		$this->assign(
			array(
			'type_list'=>array(
				'1'=>'文章',
				'2'=>'图片',
				'3'=>'视频',
			),
			'status_list'=>array(
				'0'=>'未审核',
				'1'=>'审核通过',
				'-1'=>'审核不通过',
			),
			));
		$this->display('upload_material_list');
	}
	
	//视频添加
	public function video_add(){
		
		$topmenu = $this->topmenu;
		$sidebarmenu = $this->sidebarmenu[I('get.k')];
		
		//获取分类信息
		$cate = D('VideoClassify')->do_list($where='and status !=0');
		
		$this->assign('topmenu',$topmenu);
		$this->assign('sidebarmenu',$sidebarmenu);
		$this->assign('cate',$cate);
		$this->display('video_add');
	}
	
	//视频异步提交
	public function ajax_video_add(){
		if(IS_AJAX){
			$title = I('title');
			$cid   = I('cid');
			$web_link = I('web_link');
			$thumb = I('thumb');
			
			//标题判断
			if(empty($title)){
				$this->error('请输入标题');
			}
			
			//分类判断
			if(empty($cid)){
				$this->error('请输入分类');
			}
			
			//链接判断
			if(empty($web_link)){
				$this->error('请输入链接');
			}
			
			//图片判断
			if(empty($thumb)){
				$this->error('请上传图片');
			}
			
			$thumb = ltrim($thumb,'/');
			
			//如果是视频表的话
			$video_data = array(
				'title'=>$title,
				'upload_link'=>$web_link,
				'cid'=>$cid,
				'status'=>1,
				'createtime'=>NOW_TIME,
				'video_picture'=>$thumb,
				'uid'=>0, //后台添加
			);
			
			$rc =M('Video')->add($video_data);
			if(!empty($rc)){
				$this->success('成功');
			}else{
				$this->error('失败');
			}
			
		}
	}
	
	
	//审核
	public function check_upload_material(){

		$id = I('id');
		if(empty($id)){
			header('Location:'.U('Admin/Material/upload_material_list',array('k'=>11)));
		}
		
		//获取数据
		$result = D('UploadMaterial')->upload_material_find($id);
		
		//获取分类信息
		$cate = D('VideoClassify')->do_list($where='and status !=0');
		
		if($result['type'] == '3'){
			$result['cid'] = M('UserUploadMaterialLog')->where('user_upload_material_id = %d',$result['id'])->getField('cid');
			
		}
		
		$topmenu = $this->topmenu;
		$sidebarmenu = $this->sidebarmenu[I('get.k')];

		$this->assign('result',$result);
		$this->assign('topmenu',$topmenu);
		$this->assign('sidebarmenu',$sidebarmenu);
		$this->assign('cate',$cate);
		$this->assign(
			array('type_list'=>array(
				'1'=>'文章',
				'2'=>'图片',
				'3'=>'视频',
			)));
		$this->display('check_upload_material');
	}
	
	//操作
	public function upload_material_edit(){
		if(IS_AJAX){
			
			$do_type   = I('do_type'); //审核通过与未通过的操作
			$id     = I('id');
			$cid    = I('cid',0); 
			$type   = I('type'); 
			
			if(empty($id) || empty($do_type)){
				$this->error('非法参数');
			}
			
			//用户执行操作
			$data = array(
				'is_pass'=>$do_type,
			);
			
			//如果视频没有分类
			if($type == '3' && empty($cid) && $do_type != '-1'){
				$this->error('请选择分类');
			}
			
			$rc = D('UploadMaterial')->upload_material_save($id,$data);
			
			//审核通过才执行
			if($rc !== false && $do_type == 1){
				$result = D('UploadMaterial')->upload_material_find($id);
				//插入记录表，视频在这个表
				$result_data = array(
					'uid'=>$result['uid'],
					'type'=>$result['type'],
					'upload_address'=>$result['upload_address'],
					'upload_link'=>$result['upload_link'],
					'status'=>1,
					'title'=>$result['title'],
					'createtime'=>NOW_TIME,
					'user_upload_material_id'=>$id,
					'cid'=>$cid,
				);
			
				$rc = D('UploadMaterial')->admin_materia_add($result_data);
				
				//如果是图片的话，插入图片素材表
				if($result['type'] == '2'){
					
					$img_data = array(
						'img_path'=>'/'.$result['upload_address'],
						'createtime'=>NOW_TIME,
						'is_admin_material'=>1,
					);
					
					M('ImageMaterial')->add($img_data);
				}else if($result['type'] == '3'){
					
					//如果是视频表的话
					$video_data = array(
						'title'=>$result['title'],
						'upload_link'=>$result['upload_link'],
						'cid'=>$cid,
						'status'=>1,
						'createtime'=>NOW_TIME,
						'video_picture'=>$result['video_picture'],
						'uid'=>$result['uid'],
					);
					
					M('Video')->add($video_data);
						
				}
				
				if(!empty($rc)){
					$this->success('成功');
				}else{
					$this->error('失败');
				}
			}else{
				$this->success('成功');
			}
		}
	}
	
	// 分类列表
	public function video_classify_list() {
		$topmenu = $this->topmenu;
		$sidebarmenu = $this->sidebarmenu[I('get.k')];
		
		$videoModel = D('VideoClassify');
		
		$total= $videoModel->do_count();
		$Page = $this->setPageHander($total);
		$show = $Page?$Page->show():'';
		
		
		$list = $videoModel->do_list('',$Page->firstRow.','.$Page->listRows);
		
		$this->assign('list',$list);
		$this->assign('pager',$show);
		
		$this->assign('topmenu',$topmenu);
		$this->assign('sidebarmenu',$sidebarmenu);
		$this->display('video_classify_list');
	}
	
	// 添加分类
	public function video_classify_add() {
		
		$id = I('id');
		if(!empty($id)){
			$result = D('VideoClassify')->do_find($id);
		}
		
		$topmenu = $this->topmenu;
		$sidebarmenu = $this->sidebarmenu[I('get.k')];
			
		$this->assign('result',$result);
		$this->assign('topmenu',$topmenu);
		$this->assign('sidebarmenu',$sidebarmenu);
		$this->display('video_classify_add');
	}
	
	// 添加分类操作
	public function video_classify_do() {
		if (!IS_AJAX) {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'非法操作！','field'=>''));
		}
		
		$cat_name = I('cat_name','','strip_tags,htmlspecialchars,trim');
		$cid       = I('cid');
		$status = I('status',0,'intval');
		if (empty($cat_name)) {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'栏目名称不能为空！','field'=>''));
		}
		
		$videoModel = D('VideoClassify');
		
		//添加
		if(empty($cid)){
			
			//判断栏目名是否存在
			if ($videoModel->check_name($cat_name)) {
				$this->ajaxReturn(array('error'=>1,'errmsg'=>'栏目名称已存在！','field'=>'cat_name'));
			}
			
			$data['name'] = $cat_name;
			$data['admin_id'] = session('uid');
			$data['createtime'] = NOW_TIME;
			$data['status'] = $status;
			$rc = $videoModel->do_add($data);
			
		}else{
		
		//修改
			$data['name']   = $cat_name;
			$data['status'] = $status;
			$rc = $videoModel->do_save($cid,$data);
		}
		
		if ($rc !== false) {
			$this->ajaxReturn(array('error'=>0,'errmsg'=>'操作成功！','field'=>''));
		} else {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'操作失败！','field'=>''));
		}
	}
	
	
	// 删除分类
	public function video_classify_delete() {
		if (!IS_AJAX) {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'非法操作！','field'=>''));
		}
		$id = I('id',0,'intval');
		if (!$id) {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'参数错误！','field'=>''));
		}
		$videoModel = D('VideoClassify');
		
		if ($videoModel->do_delete($id)) {
			$this->ajaxReturn(array('error'=>0,'errmsg'=>'操作成功！','field'=>''));
		} else {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'操作失败！','field'=>''));
		}
	}
	
	
	//视频列表
	public function video_list(){
		
		//搜视频标题
		$title = I('title');
		if($title){
			$where['v.title'] = array('LIKE','%' . $title. '%');
		}
		
		//状态不等于-1
		$where['v.status'] = array('NEQ','-1');
		
		//视频分类名称
		$category_name = I('category_name','','strip_tags,htmlspecialchars,trim');
		
		if(!empty($category_name)){
			$where['c.name'] = array('LIKE','%' . $category_name. '%');
			$this->assign('category_name',$category_name);
		}
		
		//时间
		$start_time = I('start_time','','strip_tags,htmlspecialchars,trim');
		$end_time = I('end_time','','strip_tags,htmlspecialchars,trim');
		
		if ($start_time !== '') {
			$start_time = strtotime($start_time);
			$this->assign('start_time',$start_time);
		}
		
		if ($end_time !== '') {
			$end_time = strtotime($end_time);
			$this->assign('end_time',$end_time);
		}
		
		if (!empty($start_time) && !empty($end_time)) {
			// 如果有开始及结束时间 则取中值
			$where['v.createtime'] = array(array('EGT', $start_time), array('ELT', $end_time));
		}else{
			if (!empty($start_time)) {
				// 有开始时间
				$where['v.createtime'] = array('EGT', $start_time);
			}elseif (!empty($end_time)) {
				// 有结束时间
				$where['v.createtime'] = array('ELT', $end_time);
			}
		}
		
		$total = D('Video')->do_count($where);
		$Page = $this->setPageHander($total);
		$show = $Page?$Page->show():'';	
		$list = D('Video')->do_list($where,$Page->firstRow.','.$Page->listRows);
		//查询用户
		if(!empty($list)){
			foreach($list as $k=>$v){
				$mobile = M('User')->where('id = %d',$v['uid'])->getField('mobile');
				$list[$k]['mobile'] = $mobile ? $mobile : '后台添加';
			}			
		}
		
		$topmenu = $this->topmenu;
		$sidebarmenu = $this->sidebarmenu[I('get.k')];
		
		$this->assign('topmenu',$topmenu);
		$this->assign('sidebarmenu',$sidebarmenu);
		$this->assign('list',$list);
		$this->assign('pager',$show);
		$this->assign('title',$title);
		$this->assign(
			array(
			'status_list'=>array(
				'0'=>'禁止',
				'1'=>'正常',
			),
			));
		$this->display('video_list');
	}
	
	
	//修改
	public function video_edit(){
		$id = I('id');
		if(empty($id)){
			header('Location:'.U('Admin/Material/video_list',array('k'=>10,'sk'=>5)));
		}
		
		$result = D('Video')->do_find($id);
		//获取分类信息
		$cate = D('VideoClassify')->do_list($where='and status !=0');
	
		$topmenu = $this->topmenu;
		$sidebarmenu = $this->sidebarmenu[I('get.k')];
		
		$this->assign('result',$result);
		$this->assign('topmenu',$topmenu);
		$this->assign('sidebarmenu',$sidebarmenu);
		$this->assign('cate',$cate);
		
		$this->display('video_edit');
	}
	
	//修改视频
	public function do_video_edit(){
		
		if(!IS_AJAX){
			return false;
		}
		
		$id  = I('id');			//修改id
		$cid = I('cid','');     //分类id
		$title = I('title',''); //标题
		$thumb = I('thumb',''); //图片
		
		if(empty($id) || empty($cid) || empty($title) || empty($thumb)){
			$this->error('缺少必填参数');
		}
	
		$data = array(
			'cid'=>$cid,
			'title'=>$title,
			'video_picture'=>$thumb,
		);
		
		$rc = D('Video')->do_save($id,$data);
		
		if($rc !== false){
			$this->success('成功');
		}else{
			$this->error('失败');
		}	
	}
	
	//删除视频
	public function do_video_delete(){
		if(!IS_AJAX){
			return false;
		}
		
		$id = I('id');
		if(empty($id)){
			$this->error('参数错误！');
		}
		
		$data = array(
			'status'=>'-1'
		);
		
		$rc = D('Video')->do_save($id,$data);
		if($rc !== false){
			$this->success('成功');
		}else{
			$this->error('失败');
		}	
	}
	
	
	//删除素材
	public function do_material_delete(){
		if(!IS_AJAX){
			return false;
		}
		$id = I('id');
		
		if(empty($id)){
			$this->error('参数错误！');
		}
		$data = array(
			'is_pass'=>'-2'
		);
		
		$rc = D('UploadMaterial')->upload_material_save($id,$data);
		
		if($rc !== false){
			$this->success('成功');
		}else{
			$this->error('失败');
		}	
	}
	
	//组装iframe视频链接
	public function display_iframe(){
		$url  = I('url');
		if(empty($url)){
			return false;
		}
	
		//QQ视频
		if(strpos($url,'v.qq.com') !== false){	
			
			$video_url = display_compose_iframe($url,'qq');
		
		//土豆视频
		}else if(strpos($url,'www.tudou.com') !== false){
			$video_url = display_compose_iframe($url,'tudou');
		
		//优酷视频
		}else if(strpos($url,'v.youku.com') !== false){
			
			$video_url = display_compose_iframe($url,'youku');
		}
	
		if(!empty($video_url)){
			$this->ajaxReturn(array('error'=>1,'url'=>$video_url));
		}else{
			$this->ajaxReturn(array('error'=>0,'url'=>$video_url));			
		}
	}
	
	
	
	
}